<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    v-if="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="服务器名称" prop="name">
          <el-input
            v-model="ruleForm.name"
            v-focus
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="操作系统" prop="systemOperation">
          <el-select
            v-model="ruleForm.systemOperation"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in systemOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="IP" prop="ip">
          <el-input
            v-model="ruleForm.ip"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="ssh协议端口" prop="sshPort">
          <el-input
            v-model="ruleForm.sshPort"
            oninput ="value=value.replace(/[^\d]/g,'')"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="账号" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="description">
          <el-input
            v-model="ruleForm.description"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')"
        @keyup.enter.native="submitForm('ruleForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      title: "新增服务器",
      dialogVisible: false,
      systemOption: [
        { id: 1, name: "linux" },
        { id: 2, name: "windows" },
      ],
      ruleForm: {
        name: "",
        ip: "",
        sshPort: "",
        userName: "",
        password: "",
        systemOperation: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入服务器名称", trigger: "blur" },
        ],
        ip: [{ required: true, message: "请输入IP地址", trigger: "blur" }],
        systemOperation: [
          { required: true, message: "请选择操作系统", trigger: "change" },
        ],
        sshPort: [{ required: true, message: "请输入端口号", trigger: "blur" }],
        userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(data) {
      this.title = "新增服务器";
      if (data) {
        this.title = "编辑服务器";
        this.ruleForm = { ...data };
        // this.ruleForm.email=this.ruleForm.email.replace(/@b1b.com/g, "")
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("save", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        email: "",
        realName: "",
      };
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
  },
  created() {},
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
